#ifndef __uart_C
#define __uart_C

#include "uart.h"  

// 接收数据
#if using_uart1 > 0
void uart_init(){	
	  GPIO_Config_T configStruct; 
    USART_Config_T USART_ConfigStruct; 
	  
	  RCM_EnableAPB2PeriphClock((RCM_APB2_PERIPH_T)(RCM_UART1_Pin | RCM_UART1));  
	  
    configStruct.mode = GPIO_MODE_AF_PP; 
    configStruct.pin = UART1_TX_PIN; 
    configStruct.speed = GPIO_SPEED_50MHz;     
    GPIO_Config(UART1_PORT, &configStruct); 

    configStruct.mode = GPIO_MODE_IN_FLOATING;   
    configStruct.pin = UART1_RX_PIN;   
    GPIO_Config(UART1_PORT, &configStruct);  

    USART_ConfigStruct.baudRate = Uart_BaudRate; 
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;   
    USART_ConfigStruct.mode = USART_MODE_TX_RX;    
    USART_ConfigStruct.parity = USART_PARITY_NONE;   
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;   
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;    

    /* USART configuration */
    USART_Config(UART1_Id, &USART_ConfigStruct); 
		
    USART_EnableInterrupt(UART1_Id, USART_INT_RXBNE); 
    NVIC_EnableIRQRequest(UART1_IRQn_flg, 4, 0); 
		
		#if uart1_DMA_send > 0
	  // 启动DMA
		USART_EnableDMA(UART1_Id, USART_DMA_TX);
	  #endif
		
		/* Enable USART */
    USART_Enable(UART1_Id);	
		
}

#if uart1_DMA_send > 0	  
void UART1_TX_DMA_Init(){
    DMA_Config_T dmaConfig;

    /* Enable DMA Clock */
    RCM_EnableAHBPeriphClock(RCM_UART1_DMA);

    /* DMA config */
    dmaConfig.peripheralBaseAddr = (USART1_BASE + 0x04);
    dmaConfig.memoryBaseAddr = (uint32_t)uart1TxDatas;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_DST;
    dmaConfig.bufferSize = rxBuf1_size;
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
    dmaConfig.loopMode = DMA_MODE_NORMAL;
    dmaConfig.priority = DMA_PRIORITY_HIGH;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;
    
    /* Enable DMA channel */
    DMA_Config(UART1_DMA_Channel, &dmaConfig);
    
}
#endif
#endif

#if using_uart2 > 0
void uart2_init(){	
	  GPIO_Config_T configStruct; 
    USART_Config_T USART_ConfigStruct; 
	  
	  RCM_EnableAPB2PeriphClock(RCM_UART2_Pin); 
    RCM_EnableAPB1PeriphClock(RCM_UART2); 	

    /* Configure USART Tx as alternate function push-pull */
    configStruct.mode = GPIO_MODE_AF_PP; 
    configStruct.pin = UART2_TX_PIN; 
    configStruct.speed = GPIO_SPEED_50MHz;  
    GPIO_Config(UART2_PORT, &configStruct); 

    /* Configure USART Rx as input floating */ 
    configStruct.mode = GPIO_MODE_IN_FLOATING; 
    configStruct.pin = UART2_RX_PIN; 
    GPIO_Config(UART2_PORT, &configStruct); 
		
    USART_ConfigStruct.baudRate = Uart_BaudRate;
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE; 
    USART_ConfigStruct.mode = USART_MODE_TX_RX;
    USART_ConfigStruct.parity = USART_PARITY_NONE;
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
		
		/* USART configuration */
    USART_Config(UART2_Id, &USART_ConfigStruct);
		
		USART_EnableInterrupt(UART2_Id, USART_INT_RXBNE);
    NVIC_EnableIRQRequest(UART2_IRQn_flg, 4, 0);	
			
    #if uart2_DMA_send > 0
	  // 启动DMA
		USART_EnableDMA(UART2_Id, USART_DMA_TX);
	  #endif
		
		/* Enable USART */
    USART_Enable(UART2_Id);
}

#if uart2_DMA_send > 0	  
void UART2_TX_DMA_Init(){
    DMA_Config_T dmaConfig;

    /* Enable DMA Clock */
    RCM_EnableAHBPeriphClock(RCM_UART2_DMA);

    /* DMA config */
    dmaConfig.peripheralBaseAddr = (USART2_BASE + 0x04);
    dmaConfig.memoryBaseAddr = (uint32_t)uart2TxDatas;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_DST;
    dmaConfig.bufferSize = rxBuf2_size;
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
    dmaConfig.loopMode = DMA_MODE_NORMAL;
    dmaConfig.priority = DMA_PRIORITY_HIGH;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;
    
    /* Enable DMA channel */
    DMA_Config(UART2_DMA_Channel, &dmaConfig);
    
}
#endif //uart2_DMA_send > 0
#endif
#if using_uart3 > 0
void uart3_init(){	
	  GPIO_Config_T configStruct; 
    USART_Config_T USART_ConfigStruct; 
	  
	  RCM_EnableAPB2PeriphClock(RCM_UART3_Pin); 
    RCM_EnableAPB1PeriphClock(RCM_UART3); 	

    /* Configure USART Tx as alternate function push-pull */
    configStruct.mode = GPIO_MODE_AF_PP; 
    configStruct.pin = UART3_TX_PIN; 
    configStruct.speed = GPIO_SPEED_50MHz;  
    GPIO_Config(UART3_PORT, &configStruct); 

    /* Configure USART Rx as input floating */ 
    configStruct.mode = GPIO_MODE_IN_FLOATING; 
    configStruct.pin = UART3_RX_PIN; 
    GPIO_Config(UART3_PORT, &configStruct); 
		
    USART_ConfigStruct.baudRate = Uart_BaudRate;
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE; 
    USART_ConfigStruct.mode = USART_MODE_TX_RX;
    USART_ConfigStruct.parity = USART_PARITY_NONE;
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
		
		/* USART configuration */
    USART_Config(UART3_Id, &USART_ConfigStruct);
		
		USART_EnableInterrupt(UART3_Id, USART_INT_RXBNE);
    NVIC_EnableIRQRequest(UART3_IRQn_flg, 4, 0);	
		
		#if uart3_DMA_send > 0
	  // 启动DMA
		USART_EnableDMA(UART3_Id, USART_DMA_TX);
	  #endif
				
		/* Enable USART */
    USART_Enable(UART3_Id);
}

#if uart3_DMA_send > 0	  
void UART3_TX_DMA_Init(){
    DMA_Config_T dmaConfig;

    /* Enable DMA Clock */
    RCM_EnableAHBPeriphClock(RCM_UART3_DMA);

    /* DMA config */
    dmaConfig.peripheralBaseAddr = (USART3_BASE + 0x04);
    dmaConfig.memoryBaseAddr = (uint32_t)uart3TxDatas;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_DST;
    dmaConfig.bufferSize = rxBuf3_size;
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
    dmaConfig.loopMode = DMA_MODE_NORMAL;
    dmaConfig.priority = DMA_PRIORITY_HIGH;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;
    
    /* Enable DMA channel */
    DMA_Config(UART3_DMA_Channel, &dmaConfig);
    
}
#endif

#endif
#if using_uart4 > 0
// 指纹初始化:
void uart4_init(){	
	  GPIO_Config_T configStruct; 
    USART_Config_T USART_ConfigStruct; 
	  
	  RCM_EnableAPB2PeriphClock(RCM_UART4_Pin);       
    RCM_EnableAPB1PeriphClock(RCM_UART4); 	

    /* Configure USART Tx as alternate function push-pull */
    configStruct.mode = GPIO_MODE_AF_PP; 
    configStruct.pin = UART4_TX_PIN;   
    configStruct.speed = GPIO_SPEED_50MHz;  
    GPIO_Config(UART4_PORT, &configStruct); 

    /* Configure USART Rx as input floating */ 
    configStruct.mode = GPIO_MODE_IN_FLOATING; 
    configStruct.pin = UART4_RX_PIN;   
    GPIO_Config(UART4_PORT, &configStruct); 
		
    USART_ConfigStruct.baudRate = Uart4_BaudRate;
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE; 
    USART_ConfigStruct.mode = USART_MODE_TX_RX;
    USART_ConfigStruct.parity = USART_PARITY_NONE;
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
		
		/* USART configuration */
    USART_Config(UART4_Id, &USART_ConfigStruct);
		
		USART_EnableInterrupt(UART4_Id, USART_INT_RXBNE);
    NVIC_EnableIRQRequest(UART4_IRQn_flg, 4, 0);	
				
		#if uart4_DMA_send > 0
	  // 启动DMA
		USART_EnableDMA(UART4_Id, USART_DMA_TX);
	  #endif
		/* Enable USART */
    USART_Enable(UART4_Id);
}

#if uart4_DMA_send > 0	  
void UART4_TX_DMA_Init(){
    DMA_Config_T dmaConfig;

    /* Enable DMA Clock */
    RCM_EnableAHBPeriphClock(RCM_UART4_DMA);

    /* DMA config */
    dmaConfig.peripheralBaseAddr = (UART4_BASE + 0x04);
    dmaConfig.memoryBaseAddr = (uint32_t)uart4TxDatas;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_DST;
    dmaConfig.bufferSize = rxBuf4_size;
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_BYTE;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_BYTE;
    dmaConfig.loopMode = DMA_MODE_NORMAL;
    dmaConfig.priority = DMA_PRIORITY_HIGH;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;
    
    /* Enable DMA channel */
    DMA_Config(UART4_DMA_Channel, &dmaConfig);
    
}
#endif

#endif
#if using_uart5 > 0
void uart5_init(){	
	  GPIO_Config_T configStruct; 
    USART_Config_T USART_ConfigStruct; 
	  
	  RCM_EnableAPB2PeriphClock(RCM_UART5_RxPin);  
    RCM_EnableAPB2PeriphClock(RCM_UART5_TxPin);	 
    RCM_EnableAPB1PeriphClock(RCM_UART5); 	

    /* Configure USART Tx as alternate function push-pull */
    configStruct.mode = GPIO_MODE_AF_PP; 
    configStruct.pin = UART5_TX_PIN; 
    configStruct.speed = GPIO_SPEED_50MHz;  
    GPIO_Config(UART5_TxPORT, &configStruct); 

    /* Configure USART Rx as input floating */ 
    configStruct.mode = GPIO_MODE_IN_FLOATING; 
    configStruct.pin = UART5_RX_PIN; 
    GPIO_Config(UART5_RxPORT, &configStruct); 
		
    USART_ConfigStruct.baudRate = Uart_BaudRate;  
    USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;     
    USART_ConfigStruct.mode = USART_MODE_TX_RX;  
    USART_ConfigStruct.parity = USART_PARITY_NONE;  
    USART_ConfigStruct.stopBits = USART_STOP_BIT_1;  
    USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;  
		
		/* USART configuration */
    USART_Config(UART5_Id, &USART_ConfigStruct);  
		
		USART_EnableInterrupt(UART5_Id, USART_INT_RXBNE); 
    NVIC_EnableIRQRequest(UART5_IRQn_flg, 4, 0);		
		
		/* Enable USART */
    USART_Enable(UART5_Id);
		
}
#endif

// 串口打印数据：类似printf功能：
uint8_t uart_printfDatas(USART_T* usart,const char *format,...){
	  uint16_t timeout = 50000;
	  uint16_t i = 0;    
		va_list args;
    char dataArr[txBuf_size];
    va_start(args, format);
	  vsprintf(dataArr,format,args);
    va_end(args);
	  for(i = 0; i < txBuf_size && dataArr[i] != '\0'; i++){
			  USART_TxData(usart,dataArr[i]);
        while(USART_ReadStatusFlag(usart,USART_FLAG_TXBE) == RESET && timeout--);
		}           
    return i;
}

#if uart1_DMA_send > 0 || uart2_DMA_send > 0 || uart3_DMA_send > 0 || uart4_DMA_send > 0
// 串口发送数据：
uint8_t uart_DMA_sendDatas(DMA_Channel_T *UART_DMA_Channel,DMA_FLAG_T UART_DMA_FLAG_TC,uint8_t *dataScr,uint16_t datalen){	  
	  uint16_t timeout = 50000;
		// 先关闭DMA：方便设置DMA参数;
    DMA_Disable(UART_DMA_Channel); 
		// DMA的dmaConfig.memoryBaseAddr设置;
    UART_DMA_Channel -> CHMADDR = (uint32_t)dataScr; 
	  //传播数据设置
    UART_DMA_Channel -> CHNDATA = datalen; // DMA的dmaConfig.bufferSize设置;
	  //DMA启动数据发送
    DMA_Enable(UART_DMA_Channel); // 启动DMA数据传递;
	  // 等待数据发送完成;
    while((DMA_ReadStatusFlag(UART_DMA_FLAG_TC) == RESET) && timeout--){
		}	
		return datalen;
}
#endif

uint8_t uart_sendDatas(USART_T* uart,uint8_t *dataScr,uint16_t datalen){
    uint16_t i = 0;  
	  uint16_t timeout = 50000;
	  for(i = 0; i < datalen; i++){
			  USART_TxData(uart,dataScr[i]);
        while(USART_ReadStatusFlag(uart,USART_FLAG_TXBE) == RESET && timeout--);
		}    
		return i;
}

#endif


